package com.example.user.web;

import lombok.AllArgsConstructor;
import lombok.Data;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;

@RestController
public class UserAction {

    @RequestMapping("user")
    public String index(HttpServletRequest request){
        return "user : " + request.getServerPort();
    }

    // 远程访问 index 工程
    @Resource
    RestTemplate restTemplate;
    @RequestMapping("index")
    public String user(){
        String ret = restTemplate.getForObject(
                // 用 服务名 替代 ip + 端口
                "http://index/index", String.class);
        return "远程访问index: " + ret;
    }

    @RequestMapping("hello")
    public String hello(String name){
        return "hello " + name;
    }

    @RequestMapping("add")
    public int add(int a, int b){
        return a + b;
    }

    @RequestMapping("reg")
    public User reg(@RequestBody User user){
        // 模拟数据处理过程, 数据模型产生变化
        user.setName(user.getName() + " xxx");
       // user.setAge(user.getAge() + 10);
        return user;
    }

    @RequestMapping("path/{ptest}")
    public String ptest(@PathVariable("ptest") String ptest){
        return ptest + " demo";
    }


}

// 用户实体类
@Data
@AllArgsConstructor
class User {
    private String name;
    private Integer age;
    private Date birthday;
    private String[] likes;
}